package com.jdk.demo.nio.bytebuffer;

import java.nio.ByteBuffer;

public class TestByteBuffer2 {
	
	private static ByteBuffer byteBuffer = null;  
    public  static void main(String[] args) {  
            /* 以下顺序不要改变*/  
            initByteBuffer();  
            testByte();   
            testChar();   
            testInt();  
            // rewind：重置mark并将potion设置到起始位置0
            testRewind();   
            testByte();
            testChar();   
            testInt();  
            testCompact();  
    }  
    /** 
     * 初始化缓存空间 
		position:0
		limit:32
		capacity:32
     */  
     public static void initByteBuffer(){  
         byteBuffer  = ByteBuffer.allocate(32);  
         System.out.println("===============init status============");  
         System.out.println("position:"+byteBuffer.position());  
         System.out.println("limit:"+byteBuffer.limit());  
         System.out.println("capacity:"+byteBuffer.capacity());  
      }  
     /** 
      * 测试Byte，占用一个字节 
      * position:1
		limit:32
		capacity:32
		======get byte:102
      */  
     public static void testByte(){  
         System.out.println("===============put byte============");  
         //字节  
         byte bbyte = 102;  
         // java.nio.HeapByteBuffer[pos=1 lim=32 cap=32]
         byteBuffer.put(bbyte);//ByteBuffer  
         byteBuffer.get(0);//byte  
         System.out.println("position:"+byteBuffer.position());  
         System.out.println("limit:"+byteBuffer.limit());  
         System.out.println("capacity:"+byteBuffer.capacity());  
         System.out.println("======get byte:"+byteBuffer.get(0));  
     }  
     /** 
      * 测试Char，占用2个字节 
      * position:3
		limit:32
		capacity:32
		======get Char:a
      */  
     public static void testChar(){  
    	 System.out.println("===============put char============");  
         //字符  
         char aChar= 'a';  
         // java.nio.HeapByteBuffer[pos=3 lim=32 cap=32]
         byteBuffer.putChar(aChar);  
         System.out.println("position:"+byteBuffer.position());  
         System.out.println("limit:"+byteBuffer.limit());  
         System.out.println("capacity:"+byteBuffer.capacity());  
         System.out.println("======get Char:"+byteBuffer.getChar(1));  
     }  

      /** 
       * 测试int，占用4个字节 
       * position:7
		limit:32
		capacity:32
		======get int:4
       */  
      public static void testInt(){  
          System.out.println("===============put int============");  
          //int  
          int int4 = 4;  
          byteBuffer.putInt(int4);  
          System.out.println("position:"+byteBuffer.position());  
          System.out.println("limit:"+byteBuffer.limit());  
          System.out.println("capacity:"+byteBuffer.capacity());  
          //这里为什么从第三个字节开始读取，因为前面一个字节和一个字符总共三个字节  
          System.out.println("======get int:"+byteBuffer.getInt(3));  
      }  
      
      public static void testRewind(){
    	  System.out.println("===============rewind===========");  
    	  // java.nio.HeapByteBuffer[mark=7 pos=7 lim=32 cap=32]
    	  byteBuffer.mark();
    	  // java.nio.HeapByteBuffer[mark=-1 pos=0 lim=32 cap=32]
    	  byteBuffer.rewind();
    	  System.out.println("position:"+byteBuffer.position());  
          System.out.println("limit:"+byteBuffer.limit());  
          System.out.println("capacity:"+byteBuffer.capacity());  
      }
      
      public static void testCompact(){  
             System.out.println("===============compact============");  
                /* 
                 * compact操作用于当 
                 *  while (in.read(buf) >= 0 || buf.position != 0) { 
                 *     buf.flip(); 
                 *     out.write(buf); 
                 *     buf.compact();    // In case of partial write 
                 *    } 
                 * 当out发送数据，即读取buf的数据，write方法可能只发送了部分数据，buf里还有剩余， 
                 * 这时调用buf.compact()函数将position与limit之间的数据，copy到buf的0到limit-position，进行压缩（非实际以压缩，只是移动）， 
                 * 以便下次 向写入缓存。 
                 */  
                // java.nio.HeapByteBuffer[pos=25 lim=32 cap=32]
                byteBuffer.compact();  
                System.out.println("position:"+byteBuffer.position());  
                System.out.println("limit:"+byteBuffer.limit());  
                System.out.println("capacity:"+byteBuffer.capacity());  
                System.out.println("======get int:"+byteBuffer.getInt(3));  
                System.out.println("===============flip============");  
               /*  
                * buf.put(magic);    // Prepend header 
                * in.read(buf);      // Read data into rest of buffer 
                * buf.flip();        // Flip buffer 
                * out.write(buf);  
                * 当in从缓冲中读取数据后，如果想要将缓存中的数据发送出去，则调用buf.flip()函数，limit为当前position，position为0， 
                * / 
//              byteBuffer.flip(); 
                System.out.println("===============rewind============"); 
                /*  
                * out.write(buf);    // Write remaining data 
                * buf.rewind();      // Rewind buffer 
                * buf.get(array);    // Copy data into array</pre></blockquote> 
                * 当out写出数据，即读取buf的数据后，如果想要从缓存中，从0位置，获取缓存数据，则调用buf.rewind() 
                */              
//              byteBuffer.rewind();  
                  
        }  

}
